from django.core.validators import MaxValueValidator
from django.db import models

from . import validators


class Human(models.Model):

    name = models.CharField(
        'Имя',
        max_length=50
    )
    surname = models.CharField(
        'Фамилия',
        max_length=50
    )


class Employment(models.Model):

    human = models.OneToOneField(
        Human,
        on_delete=models.CASCADE,
        related_name='employment',
        verbose_name='Человек'
    )
    is_unemployed = models.BooleanField(
        'Безработный',
        null=True,
        blank=True
    )


class AdditionalDataHuman(models.Model):

    GENDER_CHOICES = (
        ('F', 'Female'),
        ('M', 'Male'),
        ('O', 'Other'),
    )
    human = models.OneToOneField(
        Human,
        on_delete=models.CASCADE,
        related_name='additional_data',
        verbose_name='Человек'
    )
    gender = models.CharField(
        'Пол',
        max_length=1,
        choices=GENDER_CHOICES
    )
    blood_type = models.PositiveIntegerField(
        'Группа крови',
        validators=[MaxValueValidator(4)]
    )
    is_married = models.BooleanField(
        'Находится в браке'
    )


class Marriage(models.Model):

    husband = models.ForeignKey(
        Human,
        on_delete=models.CASCADE,
        related_name='husband',
        verbose_name='Муж'
    )
    wife = models.ForeignKey(
        Human,
        on_delete=models.CASCADE,
        related_name='wife',
        verbose_name='Жена'
    )
    beginning_of_marriage = models.DateTimeField(
        'Начало брака'
    )
    end_of_marriage = models.DateTimeField(
        'Конец брака',
        null=True,
        blank=True
    )
    is_marriage_valid = models.BooleanField(
        'Брак действителен',
        default=True
    )


class PlaceOfResidence(models.Model):

    owner = models.ForeignKey(
        Human,
        on_delete=models.SET_NULL,
        related_name='addresses',
        null=True,
        blank=True,
        verbose_name='Владелец'
    )
    country = models.CharField(
        'Страна',
        max_length=50
    )
    city = models.CharField(
        'Город',
        max_length=50
    )
    address = models.CharField(
        'Адрес',
        max_length=100
    )
    postal_code = models.PositiveIntegerField(
        'Почтовый индекс'
    )

    def clean(self):
        validators.check_length_of_number(self.postal_code, 6)

    def save(self, *args, **kwargs):
        self.full_clean()
        return super().save(*args, **kwargs)


class Action(models.Model):

    human = models.ForeignKey(
        Human,
        on_delete=models.CASCADE,
        related_name='actions',
        verbose_name='Человек'
    )
    description = models.TextField(
        'Описание'
    )
    evaluation = models.PositiveIntegerField(
        'Оценка',
        validators=[MaxValueValidator(10)]
    )
    date = models.DateTimeField(
        'Дата',
        auto_now=True
    )
